package lifecycle;

import aware.UserServiceImplAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    public static void main(String[] args) {
        //basicLifecycle();
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-lifecycle.xml");
        ((ConfigurableApplicationContext)context).close();
    }

    private static void basicLifecycle() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-lifecycle.xml");

        UserServiceImplLifecycle user = context.getBean("user", UserServiceImplLifecycle.class);
//        UserServiceImplLifecycle user2 = context.getBean("user", UserServiceImplLifecycle.class);

        //1.如果作用域是单例的，那么容器启动的时候，构造函数与初始化代码都会立即得到执行
        //getBean获取对象时只是返回对象给你，不会再初始化
        //容器关闭时，会销毁所有的单例bean，也就是调用destroy相关的方法

        //2.如果是原型bean，容器启动什么都不干，每getBean一次就构造函数与初始化
        //代码都会得到执行,容器销毁时，原型bean不进行任何处理，由垃圾回收处理

        ((ConfigurableApplicationContext)context).close();
    }
}
